package com.yx.changdao.web;

import com.yx.changdao.common.utils.SysConst;
import com.yx.changdao.init.InitSysMenu;
import com.yx.changdao.init.InitSysPermissionAndRole;
import com.yx.changdao.init.InitSysUser;
import com.yx.changdao.service.SysMenuService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import javax.servlet.MultipartConfigElement;

/**
 * 后台模块
 *
 * @author wangzd
 * @createtime 2019-11-05 14:57
 */
// 此处扫包使用常量 IDEA会产生未注入的报错 不影响正常运行
@ComponentScan(basePackages = {SysConst.SYS_BASE_PKG})
@SpringBootConfiguration
@EnableAutoConfiguration
// war 包必须写 ServletComponentScan
@ServletComponentScan
// MyBatisPlus Mapper所在的包
@MapperScan(basePackages = SysConst.MYBATIS_MAPPER_PKG)
public class SbAdminApplication extends SpringBootServletInitializer implements CommandLineRunner {

    /**
     * 打 war 包需要重写此方法
     *
     * @param application
     * @return
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SbAdminApplication.class);
    }

    /**
     * 入口方法
     *
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(SbAdminApplication.class);
    }

    /**
     * 系统初始化
     */
    @Autowired
    private InitSysPermissionAndRole initSysPermissionAndRole;

    /**
     * 初始化系统用户
     */
    @Autowired
    private InitSysUser initSysUser;

    /**
     * 初始化系统菜单
     */
    @Autowired
    private InitSysMenu initSysMenu;

    @Autowired
    private SysMenuService sysMenuService;

    @Value("${cusjson}")
    protected String json;

    /**
     * 系统初始化初始化方法
     *
     */
    @Override
    public void run(String... args) throws Exception {
        // 初始化权限和角色
        initSysPermissionAndRole.init();
        // 初始化系统用户
        initSysUser.init();
        // 初始化系统菜单
       // initSysMenu.init();

        initSysMenu.saveOrUpdateByJSON(json);
    }


  /*  @Bean
    public MultipartConfigElement multipartConfigElement() {
         MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setLocation("/data/tmp");
       return factory.createMultipartConfig();
    }*/


}
